FUNCTION ZFIFM_INV_GET_LIST.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_ZYSFPHM) TYPE  ZFIT079_ITEM-ZYSFPHM OPTIONAL
*"     VALUE(IV_BEGDA) TYPE  BEGDA OPTIONAL
*"     VALUE(IV_ENDDA) TYPE  ENDDA OPTIONAL
*"     VALUE(IV_ENTER_ACCOUNT_NO) TYPE  CHAR1 OPTIONAL
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"     VALUE(ET_INV_LIST) TYPE  ZCLDT_INPUT_INVOICE_LIST_Q_TAB
*"  TABLES
*"      RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
  DATA:
    LO_PI    TYPE REF TO ZCLCO_SI_SAP2NONO_INPUT_INVOIC,
    LS_OUT   TYPE ZCLMT_INPUT_INVOICE_LIST_QUER1,
    LS_INPUT TYPE ZCLMT_INPUT_INVOICE_LIST_QUERY.

  DATA:
    LV_INVOICE_CODE   TYPE STRING,
    LV_INVOICE_NUMBER TYPE STRING,
    LT_INV_LIST       TYPE ZCLDT_INPUT_INVOICE_LIST_Q_TAB,
    LS_INV_LIST       TYPE ZCLDT_INPUT_INVOICE_LIST_QUER1.

  DATA:LR_ERROR TYPE REF TO CX_ROOT.

  DATA:LV_UUID_C32 TYPE SYSUUID_C32,
       LV_TS       TYPE STRING.

  DATA: LV_SIZE       TYPE I VALUE '50',
        LV_CURR_PAGE  TYPE I,
        LV_TOTAL_PAGE TYPE I.

  DATA:LV_ZBDAY TYPE STRING,
       LV_ZEDAY TYPE STRING.

  ZIRPSAVE1: 'ZFIFM_INV_GET_LIST'. "Log

  CLEAR: ET_INV_LIST[].

  "诺诺平台要求开票时间段不能为空
  IF IV_BEGDA IS INITIAL AND IV_ENDDA IS INITIAL.
    ES_MSG-TYPE = 'E'.
    ES_MSG-MESSAGE = '请录入开票时间段'(T02).

    APPEND ES_MSG TO RETURN.
    ZIRPSAVE2: CX_R SPACE.
    RETURN.
  ENDIF.

  IF IV_ZYSFPHM IS NOT INITIAL.
    IF IV_ZYSFPHM CS '-'.
      SPLIT IV_ZYSFPHM AT '-' INTO LV_INVOICE_CODE LV_INVOICE_NUMBER.
    ENDIF.
  ENDIF.

*&--------获取配置表
  CLEAR ES_MSG.
  PERFORM FRM_GET_CONFIG CHANGING ES_MSG.
  IF ES_MSG-TYPE = 'E'.

    APPEND ES_MSG TO RETURN.
    ZIRPSAVE2: CX_R SPACE.
    RETURN.
  ENDIF.

  TRY .
      CREATE OBJECT LO_PI.

    CATCH CX_AI_SYSTEM_FAULT INTO DATA(LR_CX_AI).
      ES_MSG-TYPE = 'E'.
      ES_MSG-MESSAGE = LR_CX_AI->GET_TEXT( ).

      APPEND ES_MSG TO RETURN.
      ZIRPSAVE2: CX_R SPACE.
      RETURN.
  ENDTRY.


  CLEAR:LV_CURR_PAGE.
  LV_CURR_PAGE =  1.

  IF IV_BEGDA IS NOT INITIAL.
    CLEAR:LV_ZBDAY.
    LV_ZBDAY = IV_BEGDA+0(4) && '-' && IV_BEGDA+4(2) && '-' && IV_BEGDA+6(2).
    CONDENSE LV_ZBDAY NO-GAPS.
    LV_ZBDAY = |{ LV_ZBDAY } { '00:00:00' }|.
  ENDIF.

  IF IV_ENDDA IS NOT INITIAL.
    CLEAR:LV_ZEDAY.
    LV_ZEDAY = IV_ENDDA+0(4) && '-' && IV_ENDDA+4(2) && '-' && IV_ENDDA+6(2).
    CONDENSE LV_ZEDAY NO-GAPS.
    LV_ZEDAY = |{ LV_ZEDAY } { '23:59:59' }|.
  ENDIF.


  DO.
    TRY.
*&--------清空变量
        CLEAR:LS_OUT,LS_INPUT.
*&--------获取GUID
        CLEAR:LV_UUID_C32.
        GO_TAX->GET_GUID( IMPORTING EV_GUID_32 = LV_UUID_C32 ).
*&--------获取时间戳
        CLEAR:LV_TS.
        GO_TAX->GET_TIMESTAMP( IMPORTING EV_TS = LV_TS ).
*&--------公有参数(消息头)
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT_TYPE   = ''.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-X_NUONUO_SIGN  = ''.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-ACCESS_TOKEN   = GO_TAX->MS_ZFIT043-ZTOKEN."'6ea1d32668ce04110994bd1g9adb083i'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-USER_TAX       = GO_TAX->MS_ZFIT043-ZTAXNUM."'91430000717884624P'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-METHOD         = 'nuonuo.OpeMplatform.queryInvoicePoollist'.

*&--------公有参数(消息体)
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-SENID          = LV_UUID_C32.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-NONCE          = '503208179'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-TIMESTAMP      = LV_TS.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-APPKEY         = GO_TAX->MS_ZFIT043-ZAPPKEY."'23065770'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-APP_KEY        = GO_TAX->MS_ZFIT043-ZAPPKEY."'23065770'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-APP_SECRET     = GO_TAX->MS_ZFIT043-ZAPPSECRET."'1CF6DA0150584789'.
*&--------私有参数
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-PAYER_TAX_NUM       = GO_TAX->MS_ZFIT043-ZTAXNUM."'91430000717884624P'.
        " 签收状态默认全部：0.未签收，1.已签收
        "LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-sign_Status         = '1'.

        IF IV_ZYSFPHM IS NOT INITIAL.
          IF LV_INVOICE_NUMBER IS NOT INITIAL AND LV_INVOICE_CODE IS NOT INITIAL.
            LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-INVOICE_NUMBER = LV_INVOICE_NUMBER.
            LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-INVOICE_CODE = LV_INVOICE_CODE.
          ELSE.
            LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-ELEC_INVOICE_NUMBER = IV_ZYSFPHM.
          ENDIF.
        ENDIF.

        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-INVOICE_TIME_START  = LV_ZBDAY." '2024-03-06 23:59:59'.
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-INVOICE_TIME_END    = LV_ZEDAY."'2024-01-01 00:00:00'.

        " 页码
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-PAGE_NO             = LV_CURR_PAGE.
        CONDENSE LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-PAGE_NO NO-GAPS.
        " 每页数据量
        LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-PAGE_SIZE           = LV_SIZE.
        CONDENSE LS_OUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CONTENT-PAGE_SIZE NO-GAPS .

        CALL METHOD LO_PI->SI_SAP2NONO_INPUT_INVOICE_LST
          EXPORTING
            OUTPUT = LS_OUT
          IMPORTING
            INPUT  = LS_INPUT.

        CONDENSE LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CODE.

        IF LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-CODE = GC_NONO_RET_CODE_E0000.
          APPEND LINES OF LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-RESULT-INVOICES TO LT_INV_LIST.
        ELSE.
          ES_MSG-TYPE = 'E'.
          ES_MSG-MESSAGE = LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-DESCRIBE.
          EXIT.
        ENDIF.

        IF LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-RESULT-TOTAL_COUNT MOD LV_SIZE = 0.
          LV_TOTAL_PAGE = LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-RESULT-TOTAL_COUNT / LV_SIZE.
        ELSE.
          LV_TOTAL_PAGE = LS_INPUT-MT_INPUT_INVOICE_LIST_QUERY_RE-RESULT-TOTAL_COUNT DIV LV_SIZE.
          LV_TOTAL_PAGE = LV_TOTAL_PAGE + 1.
        ENDIF.

        IF LV_CURR_PAGE >= LV_TOTAL_PAGE OR
           LV_TOTAL_PAGE = 0.
          EXIT. " 当前页码大于等于总页码时,退出接口调用
        ENDIF.

        LV_CURR_PAGE = LV_CURR_PAGE + 1.
      CATCH CX_ROOT INTO LR_ERROR.
        ES_MSG-TYPE = 'E'.
        ES_MSG-MESSAGE =  LR_ERROR->GET_TEXT( ).

        EXIT.
    ENDTRY.
  ENDDO.

  IF ES_MSG-TYPE = 'E'.

    APPEND ES_MSG TO RETURN.
    ZIRPSAVE2: CX_R SPACE.
    RETURN.
  ENDIF.

  "读取常量配置表
  DATA:
    LT_R_INVLINE_EXCLUDE          TYPE RANGE OF STRING WITH HEADER LINE,
    LT_R_INVSTYPE_EXCLUDE         TYPE RANGE OF STRING WITH HEADER LINE,
    LV_ZINVOICELINE_EXCLUDE_VALUE TYPE /PTGWFI/Z_CONST-ZVALUE,
    LV_ZSPECIINVTYP_EXCLUDE_VALUE TYPE /PTGWFI/Z_CONST-ZVALUE.

  CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
    EXPORTING
      CONSTNAME          = 'ZINVOICELINE_EXCLUDE'
      PRODUCTCODE        = '901'
    IMPORTING
      CONSTVALUE         = LV_ZINVOICELINE_EXCLUDE_VALUE
    EXCEPTIONS
      CONSTANT_NOT_FOUND = 1
      OTHERS             = 2.

  IF LV_ZINVOICELINE_EXCLUDE_VALUE IS NOT INITIAL.
    SPLIT LV_ZINVOICELINE_EXCLUDE_VALUE AT ',' INTO TABLE DATA(LT_VALUE).
    LOOP AT LT_VALUE INTO DATA(LS_VALUE).
      CONDENSE LS_VALUE.
      IF LS_VALUE IS NOT INITIAL.

        CLEAR LT_R_INVLINE_EXCLUDE.
        LT_R_INVLINE_EXCLUDE-SIGN = 'I'.
        LT_R_INVLINE_EXCLUDE-OPTION = 'EQ'.
        LT_R_INVLINE_EXCLUDE-LOW = LS_VALUE.
        APPEND LT_R_INVLINE_EXCLUDE.
      ENDIF.

    ENDLOOP.
  ENDIF.


  CALL FUNCTION '/PTGWFI/CP_READCONSTANT'
    EXPORTING
      CONSTNAME          = 'ZSPECIINVTYP_EXCLUDE'
      PRODUCTCODE        = '901'
    IMPORTING
      CONSTVALUE         = LV_ZSPECIINVTYP_EXCLUDE_VALUE
    EXCEPTIONS
      CONSTANT_NOT_FOUND = 1
      OTHERS             = 2.

  IF LV_ZSPECIINVTYP_EXCLUDE_VALUE IS NOT INITIAL.
    REFRESH LT_VALUE[].
    SPLIT LV_ZSPECIINVTYP_EXCLUDE_VALUE AT ',' INTO TABLE LT_VALUE.
    LOOP AT LT_VALUE INTO LS_VALUE.
      CONDENSE LS_VALUE.
      IF LS_VALUE IS NOT INITIAL.

        CLEAR LT_R_INVSTYPE_EXCLUDE.
        LT_R_INVSTYPE_EXCLUDE-SIGN = 'I'.
        LT_R_INVSTYPE_EXCLUDE-OPTION = 'EQ'.
        LT_R_INVSTYPE_EXCLUDE-LOW = LS_VALUE.
        APPEND LT_R_INVSTYPE_EXCLUDE.
      ENDIF.

    ENDLOOP.
  ENDIF.

  "如果有配置过滤掉相应的发票种类、特定业务类型，则不返回该发票
  LOOP AT LT_INV_LIST INTO LS_INV_LIST.
    CONDENSE:
      LS_INV_LIST-INVOICE_LINE,
      LS_INV_LIST-SPECIAL_INVOICE_TYPE,
      LS_INV_LIST-ENTER_ACCOUNT_STATUS.

    IF LT_R_INVLINE_EXCLUDE[] IS NOT INITIAL.
      IF LS_INV_LIST-INVOICE_LINE IN LT_R_INVLINE_EXCLUDE[].
        CONTINUE.
      ENDIF.
    ENDIF.

    IF LT_R_INVSTYPE_EXCLUDE[] IS NOT INITIAL.
      IF LS_INV_LIST-SPECIAL_INVOICE_TYPE IN LT_R_INVSTYPE_EXCLUDE[].
        CONTINUE.
      ENDIF.
    ENDIF.

    IF IV_ENTER_ACCOUNT_NO IS NOT INITIAL."未入账或入账撤销
      "税局入账状态（01未入账 02已入账-企业所得税税前扣除 03已入账-企业所得税不扣除 06-入账撤销）
      IF LS_INV_LIST-ENTER_ACCOUNT_STATUS IS INITIAL OR LS_INV_LIST-ENTER_ACCOUNT_STATUS = '01' OR LS_INV_LIST-ENTER_ACCOUNT_STATUS = '06'.
        APPEND LS_INV_LIST TO ET_INV_LIST.
      ENDIF.
    ELSE.
      APPEND LS_INV_LIST TO ET_INV_LIST.
    ENDIF.

*    " s:专用发票(纸质) ，b:专用发票(电子)
*    " bs-电子发票（增值税专用发票),es:数电纸质专用发票，
*    "p:普通发票(电子) ， c:普通发票(纸质)
*    "pc-电子发票（增值税普通发票） ec:数电纸质普通发票
*    IF LS_INV_LIST-INVOICE_LINE = 's' OR
*       LS_INV_LIST-INVOICE_LINE = 'b' OR
*       LS_INV_LIST-INVOICE_LINE = 'bs' OR
*       LS_INV_LIST-INVOICE_LINE = 'es' OR
*       LS_INV_LIST-INVOICE_LINE = 'p' OR
*       LS_INV_LIST-INVOICE_LINE = 'c' OR
*       LS_INV_LIST-INVOICE_LINE = 'pc' OR
*       LS_INV_LIST-INVOICE_LINE = 'ec'.
*      APPEND LS_INV_LIST TO ET_INV_LIST.
*
*    ENDIF.
  ENDLOOP.

  ES_MSG-TYPE = 'S'.
  APPEND ES_MSG TO RETURN.
  ZIRPSAVE2: CX_R SPACE.
ENDFUNCTION.
